package com.yixun.lbx.service.impl;

import com.yixun.lbx.entity.YxProProductYield;
import com.yixun.lbx.mapper.farm.ProductYieldMapper;
import com.yixun.lbx.service.LbxRedisService;
import com.yixun.lbx.service.ProductYieldService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class ProductYieldServiceImpl implements ProductYieldService {
    private final ProductYieldMapper yieldMapper;
    private final LbxRedisService redisService;

    public ProductYieldServiceImpl(ProductYieldMapper yieldMapper, LbxRedisService redisService) {
        this.yieldMapper = yieldMapper;
        this.redisService = redisService;
    }

    @Override
    public Integer getCount(HashMap<String, Object> map) {
        return yieldMapper.getCount(map);
    }

    @Override
    public List<YxProProductYield> getListByPage(HashMap<String, Object> map) {
        return yieldMapper.getListByPage(map);
    }

    @Override
    public int addProductYield(HashMap<String, Object> map) {
        String qyCode = map.get("qyCode").toString();
        String productNo = redisService.getKey("productNo");
        if (productNo.equals("") || productNo.equals("null")) {
            String maxNo = yieldMapper.getMaxNo();
            if (maxNo == null || maxNo.equals("")) {
                maxNo = "1000";
            }
            redisService.setKeyIncrValue("productNo", Integer.valueOf(maxNo));
        }
        Long no = redisService.incr("productNo");
        map.put("productNo", qyCode + no);
        map.put("addTime", getDate());
        return yieldMapper.insertSelective(map);
    }

    @Override
    public int updateProductYield(HashMap<String, Object> map) {
        map.put("updateTime", getDate());
        return yieldMapper.updateByPrimaryKeySelective(map);
    }

    @Override
    public int deleteProductYield(HashMap<String, Object> map) {
        return yieldMapper.deleteByPrimaryKey(map);
    }

    @Override
    public List<YxProProductYield> getObjByArgs(YxProProductYield proProductYield) {
        return yieldMapper.getObjByArgs(proProductYield);
    }

    @Override
    public List<YxProProductYield> getSearchListByPage(HashMap<String, String> map) {
        return yieldMapper.getSearchListByPage(map);
    }
    
    @Override
    public void deleteProductYieldByCpId(HashMap<String, Object> map) {
        yieldMapper.deleteProductYieldByCpId(map);
    }
    
    private String getDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date());
    }
}
